import React from 'react';

import BaseModelNew from './BaseModelNew';
import Config from '../config';

export default class NewsModel extends BaseModelNew {
    constructor() {
      super();
      this.sendRequestContent = this.sendRequestContent.bind(this);
      this.requestComment = this.requestComment.bind(this);
      this.sendRequestList = this.sendRequestList.bind(this);
      this.sendComment = this.sendComment.bind(this);
      this.sendLike = this.sendLike.bind(this);
      this.sendRequestMyNewsNum = this.sendRequestMyNewsNum.bind(this);
      this.sendRequestMyNews = this.sendRequestMyNews.bind(this);
      this.sendRequestSearchList = this.sendRequestSearchList.bind(this);
      this.sendRequestNewsByTopics = this.sendRequestNewsByTopics.bind(this);
      this.sendCommentFollow = this.sendCommentFollow.bind(this);
      this.sendCommentShow = this.sendCommentShow.bind(this);
    }

    //activity_id
    //请求内容
    sendRequestContent(newsData, callback) {
      //http://118.89.111.124:8080/dqapp/news/111
      var url = Config.serverUrl + "/news/" + newsData.news_id;
      this.sendGet(url, callback)
    }

    //请求评论
    //page
    //page_size
    //post_id 帖子ID
    //http://118.89.111.124:8080/dqapp/news/1/comments?page=1&page_size=5
    requestComment(data, callback) {
      this.sendGet(Config.serverUrl + '/news/' + data.news_id + "/comments?page=" + data.page + "&page_size=" + data.page_size,
        callback);
    }

    //请求列表
    //http://118.89.111.124:8080/dqapp/news?page=1&page_size=10&sort_id=1&tag=1
    sendRequestList(data, callback) {
      let page_size = 10;
      if(data.page_size != undefined) {
        page_size = data.page_size;
      }
      let home_page = 0;
      if(data.home_page != undefined) {
        home_page = data.home_page;
      }
      var url = Config.serverUrl + "/news?page=" + data.page + "&page_size=" + page_size +
        "&sort_id=2&tag=" + data.tag + "&home_page=" + home_page;
      if(data.topics != undefined && data.topics.length > 0) {
        for (var i = 0; i < data.topics.length; i++) {
          url += "&topics=" + data.topics[i];
        }
      }
      this.sendGet(url, callback);
    }

    //搜索新闻
    sendRequestSearchList(data, callback) {
      var url ="";
      if (data.type) {
        if (data.type==='news') {
          url=Config.serverUrl + "/news?page=" + data.page + "&page_size=" + data.page_size +
          "&sort_id=2&tag=0&home_page=0&state=-1&totalItems=0&totalPage=1";
        }else if (data.type==='activities') {
          url=Config.serverUrl + "/activities/search?page=" + data.page + "&page_size=" + data.page_size +
          "&sort=2&type=1";
        }
        
        if(data.key != undefined) {
          url += "&key=" + data.key;
        }
        this.sendGet(url, callback);
      }
    }

    //发表评论
    //http://118.89.111.124:8080/dqapp/news/1/comments
    sendComment(data, callback) {
      var commentData = {
        "content": data.comment,
      }
      this.sendPost(Config.serverUrl + '/news/' + data.news_id + '/comments', commentData, callback);
    }
    //发表新闻追评
    //http://118.89.111.124:8080/dqapp/news/1/comments
    sendCommentFollow(data, callback) {
      var commentData = {
        "content": data.comment,
        "to":data.by_user_id
      }
      this.sendPost(Config.serverUrl + '/news/' + data.news_id + '/comments/'+data.comment_id+'/follow', commentData, callback);
    }
    sendCommentShow(data, callback) {
      var url = Config.serverUrl + '/news/' + data.news_id + '/comments/show';
      if (data.comment_id) {
        url += "?comment_id=" + data.comment_id;
      }else if (data.follow_id){
        url += "?follow_id=" + data.follow_id;
      }
      this.sendGet(url, callback);
    }
    //点赞
    //http://118.89.111.124/dqapp/news/11111/like
    sendLike(data, callback) {
      this.sendPutNew(Config.serverUrl + '/news/' + data.news_id + '/like', callback);
    }

    //获取推荐我的新闻数
    //http://118.89.111.124/dqapp/news/mynews/num
    sendRequestMyNewsNum(data, callback) {
      var url = Config.serverUrl + '/msgs/remind';
      this.sendGet(url, callback);
    }

    //我的新闻
    //http://118.89.111.124/dqapp/news/mynews?page=11&page_size=5
    sendRequestMyNews(data, callback) {
      this.sendGet(Config.serverUrl + '/news/mynews?page_size=10&page=' + data.page, callback);
    }

    //专题新闻列表
    sendRequestNewsByTopics(data, callback) {
      this.sendRequestList(data, callback);
    }
}
